define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Couponlist, $timeout, $ionicSlideBoxDelegate,GoPage, Auth, GoDialog, $window,$ionicPopup) {
		
		$scope.selectIndex = 0;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			// 计算滚动条
			$scope.myObj = {
				"height": all - 44 -42 - 47 + "px"
			};
			$scope.init_data($scope.selectIndex+1);
		}, false);
		
		//滑动时候触发
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
			$scope.init_data($scope.selectIndex+1);
		};
		
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
			$scope.init_data($scope.selectIndex+1);
		};
	
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
	
		//数据初始化。1：全部   2：已使用  3：已过期
		$scope.init_data = function(status) {
			Couponlist.getlist(status,1,function(data) {
				$scope.data = data.result.coupon;
				$scope.datanoused = data.result.count.datanoused;
				$scope.dataused = data.result.count.dataused;
				$scope.datainvalid = data.result.count.datainvalid;
			});
		}
		
		//查看规则
		var myPopup;
		$scope.showPopup = function(arrIndex) {
			$scope.rule = $scope.data[arrIndex]; //必须这样写
			myPopup = $ionicPopup.show({
				templateUrl: 'templates/modal/showRule_coupon.html',
				title: "优惠券使用规则",
				scope: $scope, //绑定数据，必须写这行，
				buttons: [{
					text: "确定",
					type: "button-assertive",
					onTap: function() { // 当点击保存按钮时，执行该函数

					}
				}]
			});
		};
		//点击其他区域关闭弹出窗
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {
			if(event.target.nodeName === 'HTML') {
				if(myPopup) {
					myPopup.close();
				}
			}
		});
		
		//店铺首页
		$scope.onshow_first = function(store_id) {
			if(myPopup) {
				myPopup.close();
			}
			GoPage.go_states("first", {"store_id": store_id});
		}
		//商品详情
		$scope.onshow_productdetail = function(goods_id) {
			if(myPopup) {
				myPopup.close();
			}
			GoPage.go_states("productdetail", {
				"goods_id": goods_id,
				"type": "goods",
				"limit": 0
			});
		}
		
		/*******************上下拉刷新*******************/
		$scope.nPage = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function(){
			$scope.isLoad = true;
			Couponlist.getlist($scope.selectIndex+1,1,function(data) {
				if(data!=null){
					$scope.data = data.result.coupon;
				}else{
					$scope.data = new Array();
				}
				$scope.nPage = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		
		$scope.doRefreshMore = function(){
			$scope.isLoad = true;
			if($scope.selectIndex==0){//未使用
				$scope.count = $scope.datanoused;
			}else if($scope.selectIndex==1){//已使用
				$scope.count = $scope.dataused;
			}else{//已过期
				$scope.count = $scope.datainvalid;
			}
			if($scope.count<=$scope.data.length){//如果当前的数据长度大于等于数据总数， 就不刷新了
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
				return;
			}
			Couponlist.getlist($scope.selectIndex+1,$scope.nPage+1,function(data) {
				if(data.result.coupon.length>0){
					if($scope.data!=undefined){
						$scope.data = $scope.data.concat(data.result.coupon);
						$scope.nPage += 1;
					}else{
						$scope.data = data.result.coupon;
					}
				}
			},function(){
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		/*******************上下拉刷新*******************/
		

	}

	controller.$inject = ['$scope', 'Couponlist', '$timeout', '$ionicSlideBoxDelegate','GoPage', 'Auth', 'GoDialog','$window','$ionicPopup'];
	
	return app.registerController("couponlistctrl",controller);
});